﻿using System;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using InputManager.InputHooks;
using System.Windows.Interop;

namespace InputManager.View
{
    public partial class InputWindow
    {
        public RawInputHook RawInputDevice;
        private ushort _brakKey;
        Message _message = new Message();

        public InputWindow(ushort brakeKey)
        {
            _brakKey = brakeKey;
            Activate();
            InitializeComponent();
        }

        public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (RawInputDevice != null)
            {
                // I could have done one of two things here.
                // 1. Use a Message as it was used before.
                // 2. Changes the ProcessMessage method to handle all of these parameters(more work).
                //    I opted for the easy way.

                //Note: Depending on your application you may or may not want to set the handled param.

                _message.HWnd = hwnd;
                _message.Msg = msg;
                _message.LParam = lParam;
                _message.WParam = wParam;

                RawInputDevice.ProcessMessage(_message);
            }
            return IntPtr.Zero;
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            StartWndProcHandler();
            base.OnSourceInitialized(e);
        }

        void StartWndProcHandler()
        {
            IntPtr hwnd = IntPtr.Zero;
            //Window myWin = Application.Current.MainWindow;
            Window myWin = this;

            try
            {
                hwnd = new WindowInteropHelper(myWin).Handle;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            //Get the Hwnd source   
            HwndSource source = HwndSource.FromHwnd(hwnd);
            //Win32 queue sink
            source.AddHook(new HwndSourceHook(WndProc));

            RawInputDevice = new RawInputHook(source.Handle, _brakKey);
            RawInputDevice.EscapeKeyPressed += UnFocuseWindow;
        }

        private void UnFocuseWindow(object sender, EventArgs e)
        {
            // TODO add end key to unfocuse   
        }

        private void WindowActivated(object sender, EventArgs e)
        {
            var rectangle = new Rectangle((int) Left + 10, (int)Top + 30, (int)Width - 20, (int)Height - 40);

            System.Windows.Forms.Cursor.Clip = rectangle;
            System.Windows.Forms.Cursor.Hide(); 

            RawInputDevice.IsActive = true;
        }

        private void WindowDeactivated(object sender, EventArgs e)
        {
            RawInputDevice.IsActive = false;
            System.Windows.Forms.Cursor.Show();
        }

        protected override void OnClosed(EventArgs e)
        {
            RawInputDevice.UnRegisterDevice();
            base.OnClosed(e);
        }
    }
}